<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0px" size="mini" placeholder="请输入认证专家的名称" v-model="form.searchTitle" @change="handlePageInit"/>
      <el-select class="filter-item" style="width: 120px" size="mini" placeholder="请选择发布状态" v-model="form.searchStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePage">新增</el-button>
      <el-button type="success" size="mini" icon="el-icon-share" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量认证</el-button>
    </div>
    <!--数据列表-->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="expertId" label="认证数据ID" width="280" align="center"/>
        <el-table-column prop="expertNickName" label="认证专家名称" width="120" align="center">
          <template slot-scope="scope">
            <span class="link-type">{{ scope.row.expertNickName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="expertHeadImage" label="认证专家头像" width="120" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.expertHeadImage != null && scope.row.expertHeadImage !== ''">
                <el-button size="small" width="120" type="success">预览</el-button>
              </span>
            <span v-else >无</span>
          </template>
        </el-table-column>
        <el-table-column prop="expertStatus" label="认证专家状态" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.expertStatus === 0"><el-tag type="warning">未通过</el-tag></span>
            <span v-if="scope.row.expertStatus === 1"><el-tag type="success">通过</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="expertPositions" label="认证专家职位" width="120" align="center"/>
        <el-table-column prop="expertAskPrice" label="专家提问价格" width="120" align="center"/>
        <el-table-column prop="expertAdditionalCount" label="免费追问次数" width="160" align="center"/>
        <el-table-column prop="expertShowPrice" label="专家展示价格(特价效果)" width="180" align="center"/>
        <el-table-column prop="expertShowIndex" label="专家排序" width="160" align="center"/>
        <el-table-column prop="expertCreateTime" label="认证时间" width="180" align="center"/>
        <el-table-column prop="expertTimeOut" label="过期时间" width="180" align="center"/>
        <el-table-column fixed="right" label="操作" width="420" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpenEditPage(scope)">编辑</el-button>
            <el-button v-if="scope.row.expertStatus === 0" size="mini" type="success" @click="handleUpdateDataStatus(scope, 1)">发布</el-button>
            <el-button v-if="scope.row.expertStatus === 1" size="mini" type="info" @click="handleUpdateDataStatus(scope, 0)">下架</el-button>
            <el-button type="success" size="mini" @click="handleOpenPushClassPage(scope)">课程推送</el-button>
            <el-button type="warning" size="mini" @click="handleOpenDraftClassPage(scope)">课程下架</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
    <!--专家课程推送隐藏域-->
    <el-dialog title="课程推送" :visible.sync="pageData.pushClassStatus" size="tiny"
               :before-close="handleClosePushClassPage">
      <el-form ref="form.pushClass" :model="form.pushClass" label-width="120px">
        <el-form-item prop="programType" label="数据类型">
          <el-radio v-model="form.pushClass.programType" :label="1">成套视频</el-radio>
          <el-radio v-model="form.pushClass.programType" :label="2">单集视频</el-radio>
        </el-form-item>
        <el-form-item prop="programType" label="发布状态">
          <el-radio v-model="form.pushClass.programStatus" :label="0">草稿</el-radio>
          <el-radio v-model="form.pushClass.programStatus" :label="1">发布</el-radio>
        </el-form-item>
        <el-form-item prop="programType" label="展示顺序">
          <el-input v-model="form.pushClass.programShowIndex"></el-input>
        </el-form-item>
        <el-form-item prop="columnData" label="内容数据">
          <el-select v-model="form.pushClass.columnData" multiple filterable remote reserve-keyword placeholder="请输入关键词"
                     :remote-method="handleRemoteData">
            <el-option v-for="item in responseData.remoteSearchList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlePushClassButtonClick">推送课程</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--专家课程下架隐藏域-->
    <el-dialog title="课程下架" :visible.sync="pageData.draftClassStatus" size="tiny"
               :before-close="handleCloseDraftClassPage">
      <el-table
        :data="responseData.professClassList"
        border style="width: 100%;"
        :header-cell-style="{ background: '#545C64', color: '#ffffff' }">
        <el-table-column fixed="left" prop="expertId" label="专家ID" width="280" align="center"/>
        <el-table-column prop="programName" label="节目名称" width="280" align="center"/>
        <el-table-column prop="programType" label="节目类型" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.programType === 1"><el-tag type="primary">整套视频</el-tag></span>
            <span v-if="scope.row.programType === 2"><el-tag type="success">单个视频</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="programId" label="节目ID" width="280" align="center"/>
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleDraftClassButtonClick(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsProfessorList,batchReleaseCmsProfessorData,loadProfessClassDataList,saveProfessClassData,loadProfessClassList,
    deleteProfessClassData} from "@/api/customer";
  export default {
    name: "professor",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          searchStatusList: [
            {label: "审核状态", value: -1},
            {label: "未发布", value: 0},
            {label: "发布", value: 1}
          ],
          loading: false,
          pushClassStatus: false,
          draftClassStatus: false
        },// 页面配置数据
        form:{
          searchTitle: "",
          searchStatus: -1,
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: [],// 多选的内容
          pushClass: {
            expertId: "",
            programType: 1,
            programStatus: 1,
            programShowIndex: 0,
            columnData: ""
          },// 推送课程面板
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: [],
          remoteSearchList: [],
          professClassList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsProfessorList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.handlePageInit();// 进行数据加载
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.handlePageInit();// 进行数据加载
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].expertId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePage(){
        if (PermissionsToDetectFormUser(getPermission(), "510001")){
          this.$jump.push({path: "/customer/professorSave"});
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开专家新增页面
      handleBatchReleaseData(){
        if (PermissionsToDetectFormUser(getPermission(), "510005")){
          this.$confirm("是否发布当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              expertId: this.form.selection.toString(),
              expertStatus: 1
            };
            this.batchReleaseCmsProfessorData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布数据
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "510003")){
          this.$jump.push({path: "/customer/professorEdit", query: {expertId: scope.row.expertId}});
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开专家修改页面
      handleUpdateDataStatus(scope, status){
        if (PermissionsToDetectFormUser(getPermission(), "510005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              expertId: scope.row.expertId,
              expertStatus: status
            };
            this.batchReleaseCmsProfessorData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改发布状态
      handleOpenPushClassPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "510006")){
          // 打开推送面板
          this.pageData.pushClassStatus = true;
          // 初始化推送表单
          this.form.pushClass.expertId = scope.row.expertId;
          this.form.pushClass.programType = 1;
          this.form.pushClass.programStatus = 1;
          this.form.pushClass.programShowIndex = 0;
          this.form.pushClass.columnData = "";
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开课程推送面板
      handleClosePushClassPage(){
        this.$confirm("确认关闭？").then(response => {
          // 打开推送面板
          this.pageData.pushClassStatus = false;
          // 初始化推送表单
          this.form.pushClass.expertId = "";
          this.form.pushClass.programType = 1;
          this.form.pushClass.programStatus = 1;
          this.form.pushClass.programShowIndex = 0;
          this.form.pushClass.columnData = "";
          this.responseData.remoteSearchList = [];
        }).catch(err => {
          console.log(err);
        });
      },// 关闭课程推送面板
      handleRemoteData(query){
        if (query !== "") {
          let param = {
            searchTitle: query,
            searchType: this.form.pushClass.programType
          };
          this.loadProfessClassDataList(param);
        } else {
          this.ProfessorClassPushPanelRemoteDataList = [];
        }
      },// 课程推送面板搜索内容
      handlePushClassButtonClick(){
        if (PermissionsToDetectFormUser(getPermission(), "510006")){
          this.$confirm("是否保存当前课程数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.saveProfessClassData();
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 保存课程推送数据
      handleOpenDraftClassPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "510007")){
          let prams = {expertId: scope.row.expertId};
          this.loadProfessClassList(prams);
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开课程移除面板
      handleCloseDraftClassPage(){
        this.$confirm("确认关闭？").then(response => {
          // 打开推送面板
          this.pageData.draftClassStatus = false;
          // 初始化推送表单
          this.responseData.professClassList = [];
        }).catch(err => {
          console.log(err);
        });
      },// 关闭课程推送面板
      handleDraftClassButtonClick(scope){
        if (PermissionsToDetectFormUser(getPermission(), "510007")){
          this.$confirm("是否删除当前课程数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let prams = {
              id: scope.row.id,
              expertId: scope.row.expertId
            };
            this.deleteProfessClassData(prams);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 删除课程数据
      handleDeleteData(scope){
        this.$message.info("该功能暂未开放，如有需要请联系管理员");
      },// 删除数据按钮
      // 接口
      loadCmsProfessorList(){
        loadCmsProfessorList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取专家列表
      batchReleaseCmsProfessorData(param){
        batchReleaseCmsProfessorData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.expertStatus === 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 修改专家发布状态
      loadProfessClassDataList(param){
        loadProfessClassDataList(param).then(response => {
          if (response.code === '200'){
            this.responseData.remoteSearchList = response.data
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 远程搜索数据
      saveProfessClassData(){
        this.form.pushClass.columnData = this.form.pushClass.columnData.toString();
        saveProfessClassData(this.form.pushClass).then(response => {
          if (response.code === "200"){
            this.$message.success('课程推送成功');
            // 打开推送面板
            this.pageData.pushClassStatus = false;
            // 初始化推送表单
            this.form.pushClass.expertId = "";
            this.form.pushClass.programType = 1;
            this.form.pushClass.programStatus = 1;
            this.form.pushClass.programShowIndex = 0;
            this.form.pushClass.columnData = "";
            this.responseData.remoteSearchList = [];
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 保存课程推送数据
      loadProfessClassList(prams){
        loadProfessClassList(prams).then(response => {
          if (response.code === "200"){
            this.pageData.draftClassStatus = true;
            this.responseData.professClassList = response.data;
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 获取专家已推送课程
      deleteProfessClassData(prams){
        deleteProfessClassData(prams).then(response => {
          if (response.code === "200"){
            this.$message.success('课程下架成功');
            // 打开推送面板
            this.pageData.draftClassStatus = false;
            // 初始化推送表单
            this.responseData.professClassList = [];
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 删除课程数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
